package com.ceshiren.chnjx.wework.page.contact;

import com.ceshiren.chnjx.wework.page.BasePage;
import com.ceshiren.chnjx.wework.page.contact.member.AddOrEditMemberPage;
import com.ceshiren.chnjx.wework.page.contact.member.MemberDetailPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * @author: JJJJ
 * @date:2021/11/2 16:37
 * @Description: TODO
 */
public class ContactPage extends BasePage {
    private By addMemberBtn = new By.ByLinkText("添加成员");
    private By userListText = new By.ByCssSelector(".js_has_member") ;
    private By deleteBtn = new By.ByLinkText("删除");
    private By confirmBtn = new By.ByLinkText("确认");

    private By searchMemberInput = new By.ById("memberSearchInput");
    // 搜索结果
    private By searchResultLoc = new By.ById("search_result_wrap");


    public ContactPage(WebDriver driver){
        super(driver);
    }

    /**
     * 添加成员
     * @param account 账户
     * @param name 姓名
     * @param nickName 别名
     * @param sex 性别
     * @param phone 手机
     */
    public ContactPage addMember(String account, String name, String nickName, String sex, String phone) {
        return intoAddMemberPage().addMember(account,name,nickName,sex,phone);
    }

    /**
     * 进入添加成员页面
     * @return
     */
    private AddOrEditMemberPage intoAddMemberPage(){
        clickElement(addMemberBtn);
        return new AddOrEditMemberPage(driver);
    }

    public String getAddMemberResult() {
        return getUserList();
    }

    /**
     *
     * @return 用户列表中得文本内容
     */
    public String getUserList(){
        return getElementText(userListText);
    }

    /**
     * 删除成员
     * @param username 成员名称
     */
    public ContactPage deleteMember(String username) {
        clickElement(new By.ByXPath("//td[@title='"+username+"']/preceding-sibling::td"));
        clickElement(deleteBtn);
        clickElement(confirmBtn);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return this;
    }


    public MemberDetailPage editMember(String name,String editName,String editNickName, String editPhone) {
        return intoMemberDetailPage(name).intoEditMemberPage().editMember(editName,editNickName,editPhone);
    }

    /**
     * 进入成员详情页面
     * @return
     */
    public MemberDetailPage intoMemberDetailPage(String name){
        clickElement(new By.ByXPath("//td[@title='"+name+"']"));
        return new MemberDetailPage(driver);
    }

    /**
     * 清楚测试数据
     * @param testName 测试成员名称
     */
    public void clearMemberTestData(String testName){
        while (getUserList().contains(testName)){
            deleteMember(driver.findElements(new By.ByXPath("//td[contains(@title,'"+testName+"')]")).get(0).getText());
        }
    }

    public ContactPage searchMember(String name) {
        sendData(searchMemberInput,name);
        return this;
    }

    public String getSearchResultStr() {
        return getElementText(searchResultLoc);
    }
}
